package cn.edu.nuc.onlinestore.frame;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import UtilSever.Util;
import cn.edu.nuc.onlinestore.model.Admin;
import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.model.UserCart;
import cn.edu.nuc.onlinestore.vo.Result;

public class UserLogin extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1534206275091411704L;
	private JPanel contentPane;
	private JTextField username;
	private JPasswordField password;
	private JFrame frame;



	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserLogin frame = new UserLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	

	/**
	 * Create the frame.
	 */
	public UserLogin() {
		this.frame=this;
		setTitle("中北线在商场-登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);
		
		username = new JTextField();
		username.setBounds(151, 146, 197, 21);
		contentPane.add(username);
		username.setColumns(10);
		
		password = new JPasswordField();
		password.setBounds(151, 185, 197, 21);
		contentPane.add(password);
		
		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//得到页面的参数
				String name=username.getText();
				String pwd=new String(password.getPassword());
				Result<Client> rs=Util.SeverSelectAClient(name);
				//判断返回的result类型的code的boolean值
				if(rs.getCode())
				{
					if(name.equals(((Client)rs.getObj()).getName())&&pwd.equals(((Client)rs.getObj()).getPassword()))
					{
						System.out.println("success"+(Client)rs.getObj());
						//设置名字
						UserCart.username=((Client)rs.getObj()).getName();
						UserStore st=new UserStore();
						st.setUserLogin(frame);
						
						
						//关闭本窗口
						frame.dispose();
						//打开UserStore窗口
						st.setVisible(true);
						
					}else{
						JOptionPane.showMessageDialog(null,"用户名或密码错误","", JOptionPane.NO_OPTION);
					}
				}else{
						JOptionPane.showMessageDialog(null,"用户名或密码错误",rs.getMsg(), JOptionPane.NO_OPTION);
				}
				
				
				
			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(UserLogin.class.getResource("/image/a.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
		
		JButton button_1 = new JButton("注册");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到页面的参数
				String name=username.getText();
				String pwd=new String(password.getPassword());
				Client client=new Client(name,pwd);
				//执行注册
				boolean b=Util.SeverRegisterAClient(client);
				//判断注册是否成功
				if(b)
				{
					JOptionPane.showMessageDialog(null,"注册成功","", JOptionPane.NO_OPTION);
				}else{
					JOptionPane.showMessageDialog(null,"用户名重复，请重新注册","", JOptionPane.NO_OPTION);
					username.setText("");
					password.setText("");
				}
				
			}
		});
		button_1.setBounds(152, 216, 93, 23);
		contentPane.add(button_1);
	}
}
